package yxy.game.pm2.command.pickedtroops;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;


/****************************************
 * @name CMD_PickedTroopsUnlock
 * @description 兵符槽解锁
 * @author 梁健彬
 * @since 2022-05-19
 ***************************************/
@MessageType(proto = Command.PickedTroopsUnlock, classtype = PickedTroopsMessage.PickedTroopsUnlockRequest.class)
public class CMD_PickedTroopsUnlock extends Command<PickedTroopsMessage.PickedTroopsUnlockRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsUnlockRequest request) {
        String playerCode = messageSource.getTag();
        int camp = request.getCamp();//阵容
        String code = request.getCode();//兵符的id

        PickedTroopsMessage.PickedTroopsUnlockResponse.Builder response = PickedTroopsMessage.PickedTroopsUnlockResponse.newBuilder();

        try {
            PickedTroopsSlot pickedTroopsSolt = PickedTroopsSlot.get(playerCode, code);
            PickedTroopsModule ptsmodul = PickedTroopsModule.get(playerCode);//全部兵符
            PickedTroops pickedTroops = ptsmodul.getPts().get(camp);//当前阵容的兵符

            //如果不存在，参数错误
            if (pickedTroopsSolt == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //不同阵容不能解锁
            if (pickedTroopsSolt.getType() != camp) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //如果已经解锁则不需要再解锁
            if (pickedTroops.isOpen()) {
                send(messageSource, response, Result.PickedTroopsHadUnlock);
                return;
            }

            pickedTroops.setOpen(true);
            pickedTroops.setPickedTroopsSolt(code);
            ptsmodul.set();

            pickedTroopsSolt.setUse(true);
            PickedTroopsSlot.set(playerCode,pickedTroopsSolt);

            for (PickedTroops pts : ptsmodul.getPts().values()) {
                response.addPts(PickedTroopsHandle.getPickedTroops(playerCode, pts));//返回全部兵符
            }

            PlayTypeHandle.savePickedTroop(playerCode, PlayTypeModule.get(playerCode));
            new BattleHandle().powerUpdate(playerCode);// 战力更新

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsUnlockResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}